﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using Terraria.Audio;
using Terraria.ModLoader.Audio;

namespace Terraria.ModLoader
{
	// Token: 0x02000244 RID: 580
	public static class SoundLoader
	{
		// Token: 0x06001864 RID: 6244 RVA: 0x0041E270 File Offset: 0x0041C470
		static SoundLoader()
		{
			foreach (object obj in Enum.GetValues(typeof(SoundType)))
			{
				SoundType soundType = (SoundType)obj;
				SoundLoader.nextSound[soundType] = SoundLoader.GetNumVanilla(soundType);
				SoundLoader.sounds[soundType] = new Dictionary<string, int>();
				SoundLoader.modSounds[soundType] = new Dictionary<int, ModSound>();
			}
		}

		// Token: 0x06001865 RID: 6245 RVA: 0x0041E350 File Offset: 0x0041C550
		internal static int ReserveSoundID(SoundType type)
		{
			int result = SoundLoader.nextSound[type];
			IDictionary<SoundType, int> dictionary = SoundLoader.nextSound;
			int num = dictionary[type];
			dictionary[type] = num + 1;
			return result;
		}

		// Token: 0x06001866 RID: 6246 RVA: 0x00011ED9 File Offset: 0x000100D9
		public static int SoundCount(SoundType type)
		{
			return SoundLoader.nextSound[type];
		}

		// Token: 0x06001867 RID: 6247 RVA: 0x00011EE6 File Offset: 0x000100E6
		public static int GetSoundSlot(SoundType type, string sound)
		{
			if (SoundLoader.sounds[type].ContainsKey(sound))
			{
				return SoundLoader.sounds[type][sound];
			}
			return 0;
		}

		// Token: 0x06001868 RID: 6248 RVA: 0x00011F0E File Offset: 0x0001010E
		internal static LegacySoundStyle GetLegacySoundSlot(SoundType type, string sound)
		{
			if (SoundLoader.sounds[type].ContainsKey(sound))
			{
				return new LegacySoundStyle((int)type, SoundLoader.sounds[type][sound], SoundType.Sound);
			}
			return null;
		}

		// Token: 0x06001869 RID: 6249 RVA: 0x0041E384 File Offset: 0x0041C584
		internal static void ResizeAndFillArrays()
		{
			SoundLoader.customSounds = new SoundEffect[SoundLoader.nextSound[SoundType.Custom]];
			SoundLoader.customSoundInstances = new SoundEffectInstance[SoundLoader.nextSound[SoundType.Custom]];
			Array.Resize<SoundEffect>(ref Main.soundItem, SoundLoader.nextSound[SoundType.Item]);
			Array.Resize<SoundEffectInstance>(ref Main.soundInstanceItem, SoundLoader.nextSound[SoundType.Item]);
			Array.Resize<SoundEffect>(ref Main.soundNPCHit, SoundLoader.nextSound[SoundType.NPCHit]);
			Array.Resize<SoundEffectInstance>(ref Main.soundInstanceNPCHit, SoundLoader.nextSound[SoundType.NPCHit]);
			Array.Resize<SoundEffect>(ref Main.soundNPCKilled, SoundLoader.nextSound[SoundType.NPCKilled]);
			Array.Resize<SoundEffectInstance>(ref Main.soundInstanceNPCKilled, SoundLoader.nextSound[SoundType.NPCKilled]);
			Array.Resize<Music>(ref Main.music, SoundLoader.nextSound[SoundType.Music]);
			Array.Resize<float>(ref Main.musicFade, SoundLoader.nextSound[SoundType.Music]);
			foreach (object obj in Enum.GetValues(typeof(SoundType)))
			{
				SoundType soundType = (SoundType)obj;
				foreach (string text in SoundLoader.sounds[soundType].Keys)
				{
					int soundSlot = SoundLoader.GetSoundSlot(soundType, text);
					if (soundType != SoundType.Music)
					{
						SoundLoader.GetSoundArray(soundType)[soundSlot] = ModLoader.GetSound(text);
						SoundEffectInstance[] soundInstanceArray = SoundLoader.GetSoundInstanceArray(soundType);
						int num = soundSlot;
						SoundEffect soundEffect = SoundLoader.GetSoundArray(soundType)[soundSlot];
						soundInstanceArray[num] = (((soundEffect != null) ? soundEffect.CreateInstance() : null) ?? null);
					}
					else
					{
						Main.music[soundSlot] = (ModLoader.GetMusic(text) ?? null);
					}
				}
			}
		}

		// Token: 0x0600186A RID: 6250 RVA: 0x0041E554 File Offset: 0x0041C754
		internal static void Unload()
		{
			foreach (object obj in Enum.GetValues(typeof(SoundType)))
			{
				SoundType soundType = (SoundType)obj;
				SoundLoader.nextSound[soundType] = SoundLoader.GetNumVanilla(soundType);
				SoundLoader.sounds[soundType].Clear();
				SoundLoader.modSounds[soundType].Clear();
			}
			SoundLoader.musicToItem.Clear();
			SoundLoader.itemToMusic.Clear();
			SoundLoader.tileToMusic.Clear();
		}

		// Token: 0x0600186B RID: 6251 RVA: 0x0041E600 File Offset: 0x0041C800
		internal static bool PlayModSound(int type, int style, float volume, float pan, ref SoundEffectInstance soundEffectInstance)
		{
			SoundType soundType;
			switch (type)
			{
			case 2:
				soundType = SoundType.Item;
				break;
			case 3:
				soundType = SoundType.NPCHit;
				break;
			case 4:
				soundType = SoundType.NPCKilled;
				break;
			default:
				if (type != 50)
				{
					return false;
				}
				soundType = SoundType.Custom;
				break;
			}
			if (!SoundLoader.modSounds[soundType].ContainsKey(style))
			{
				return false;
			}
			soundEffectInstance = SoundLoader.modSounds[soundType][style].PlaySound(ref SoundLoader.GetSoundInstanceArray(soundType)[style], volume, pan, soundType);
			return true;
		}

		// Token: 0x0600186C RID: 6252 RVA: 0x00011F3D File Offset: 0x0001013D
		internal static int GetNumVanilla(SoundType type)
		{
			switch (type)
			{
			case SoundType.Item:
				return 126;
			case SoundType.NPCHit:
				return 58;
			case SoundType.NPCKilled:
				return 63;
			default:
				if (type == SoundType.Custom)
				{
					return 0;
				}
				if (type != SoundType.Music)
				{
					return 0;
				}
				return 42;
			}
		}

		// Token: 0x0600186D RID: 6253 RVA: 0x00011F6E File Offset: 0x0001016E
		internal static SoundEffect[] GetSoundArray(SoundType type)
		{
			switch (type)
			{
			case SoundType.Item:
				return Main.soundItem;
			case SoundType.NPCHit:
				return Main.soundNPCHit;
			case SoundType.NPCKilled:
				return Main.soundNPCKilled;
			default:
				if (type == SoundType.Custom)
				{
					return SoundLoader.customSounds;
				}
				return null;
			}
		}

		// Token: 0x0600186E RID: 6254 RVA: 0x00011FA2 File Offset: 0x000101A2
		internal static SoundEffectInstance[] GetSoundInstanceArray(SoundType type)
		{
			switch (type)
			{
			case SoundType.Item:
				return Main.soundInstanceItem;
			case SoundType.NPCHit:
				return Main.soundInstanceNPCHit;
			case SoundType.NPCKilled:
				return Main.soundInstanceNPCKilled;
			default:
				if (type == SoundType.Custom)
				{
					return SoundLoader.customSoundInstances;
				}
				return null;
			}
		}

		// Token: 0x040016A4 RID: 5796
		private static readonly IDictionary<SoundType, int> nextSound = new Dictionary<SoundType, int>();

		// Token: 0x040016A5 RID: 5797
		internal static readonly IDictionary<SoundType, IDictionary<string, int>> sounds = new Dictionary<SoundType, IDictionary<string, int>>();

		// Token: 0x040016A6 RID: 5798
		internal static readonly IDictionary<SoundType, IDictionary<int, ModSound>> modSounds = new Dictionary<SoundType, IDictionary<int, ModSound>>();

		// Token: 0x040016A7 RID: 5799
		internal static SoundEffect[] customSounds = new SoundEffect[0];

		// Token: 0x040016A8 RID: 5800
		internal static SoundEffectInstance[] customSoundInstances = new SoundEffectInstance[0];

		// Token: 0x040016A9 RID: 5801
		public const int customSoundType = 50;

		// Token: 0x040016AA RID: 5802
		internal static readonly IDictionary<int, int> musicToItem = new Dictionary<int, int>();

		// Token: 0x040016AB RID: 5803
		internal static readonly IDictionary<int, int> itemToMusic = new Dictionary<int, int>();

		// Token: 0x040016AC RID: 5804
		internal static readonly IDictionary<int, IDictionary<int, int>> tileToMusic = new Dictionary<int, IDictionary<int, int>>();
	}
}
